package com.hznu.jspFinal.service;

import com.hznu.jspFinal.dao.CourseMapper;
import com.hznu.jspFinal.dao.CourseStudentMapper;
import com.hznu.jspFinal.dao.CourseTeacherMapper;
import com.hznu.jspFinal.dao.UserMapper;
import com.hznu.jspFinal.entity.CourseStudent;
import com.hznu.jspFinal.entity.CourseTeacher;
import com.hznu.jspFinal.entity.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * author:wp
 */
@Service
public class CourseStudentServiceImpl implements CourseStudentService{

    @Autowired
    private CourseStudentMapper courseStudentMapper;

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addOne(CourseStudent courseStudent) {
        return courseStudentMapper.insertSelective(courseStudent);
    }

    @Override
    public int delete(Integer id) {
        return courseStudentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(CourseStudent courseStudent) {
        return courseStudentMapper.updateByPrimaryKeySelective(courseStudent);
    }

    @Override
    public CourseStudent findById(Integer id) {
        return courseStudentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> findStudentsByCourseTeacherid(Integer id) {

        List<User> users = new ArrayList();
        List<CourseStudent> courseStudents = courseStudentMapper.selectByCourseTeacherid(id);
        if(courseStudents!=null){
            for(int i=0;i<courseStudents.size();i++){
                User user = userMapper.selectByPrimaryKey(courseStudents.get(i).getStudentid());
                users.add(user);
            }
        }

      return users;
    }

    @Override
    public List<CourseTeacher> findCoursesByStudentid(Integer id) {

        List<CourseStudent> courseStudents = courseStudentMapper.selectByStudentid(id);

        List<CourseTeacher> courseTeachers = new ArrayList<>();

        if(courseStudents!=null){

            for(int i=0;i<courseStudents.size();i++){
                courseTeachers.add(courseTeacherMapper.selectByPrimaryKey(courseStudents.get(i).getCourseteacherid()));
            }
        }
        return courseTeachers;
    }
}
